package com.java110.mall.chain.cmd.partner;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.partnerAdapt.PartnerAdaptDto;
import com.java110.dto.partnerAdapt.PartnerPoolDto;
import com.java110.dto.partnerAdapt.PartnerPoolValueDto;
import com.java110.intf.chain.IPartnerAdaptV1InnerServiceSMO;
import com.java110.intf.chain.IPartnerLogV1InnerServiceSMO;
import com.java110.intf.chain.IPartnerPoolV1InnerServiceSMO;
import com.java110.intf.chain.IPartnerPoolValueV1InnerServiceSMO;
import com.java110.mall.chain.partner.IPartnerOrderVerificationAdapter;
import com.java110.po.partnerAdapt.PartnerLogPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import java.util.List;

/**
 * 提供给海博 平台订单核销
 * <p>
 * /mall/api/partner.orderVerification/992024052983520004/back
 */
@Java110Cmd(serviceCode = "partner.orderVerification")
public class OrderVerificationCmd extends Cmd {

    @Autowired
    private IPartnerPoolValueV1InnerServiceSMO partnerPoolValueV1InnerServiceSMOImpl;

    @Autowired
    private IPartnerPoolV1InnerServiceSMO partnerPoolV1InnerServiceSMOImpl;

    @Autowired
    private IPartnerAdaptV1InnerServiceSMO partnerAdaptV1InnerServiceSMOImpl;

    @Autowired
    private IPartnerLogV1InnerServiceSMO partnerLogV1InnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        Assert.hasKeyAndValue(reqJson, "refund", "未包含订单标识");
        Assert.hasKeyAndValue(reqJson, "outStationNo", "未包含商家编码");

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String state = "10002";

        String outStationNo = reqJson.getString("outStationNo");
        String appId = context.getReqHeaders().get("app-id");

        reqJson.put("appId",appId);

        PartnerPoolValueDto partnerPoolValueDto = new PartnerPoolValueDto();
        partnerPoolValueDto.setColumnValue(outStationNo);
        List<PartnerPoolValueDto> valueDtos = partnerPoolValueV1InnerServiceSMOImpl.queryPartnerPoolValues(partnerPoolValueDto);

        if (ListUtil.isNull(valueDtos)) {
            throw new CmdException("店铺不存在");
        }

        try {

            String shopId = valueDtos.get(0).getShopId();

            reqJson.put("shopId", shopId);
            reqJson.put("ppId", valueDtos.get(0).getPpId());
            PartnerPoolDto partnerPoolDto = new PartnerPoolDto();
            partnerPoolDto.setPpId(valueDtos.get(0).getPpId());
            partnerPoolDto.setShopId(reqJson.getString("shopId"));
            List<PartnerPoolDto> partnerPoolDtos = partnerPoolV1InnerServiceSMOImpl.queryPartnerPools(partnerPoolDto);
            if (ListUtil.isNull(partnerPoolDtos)) {
                state = "10003";
                throw new CmdException("合作商不存在");
            }

            PartnerAdaptDto partnerAdaptDto = new PartnerAdaptDto();
            partnerAdaptDto.setPartnerType(partnerPoolDtos.get(0).getPartnerType());
            List<PartnerAdaptDto> partnerAdaptDtos = partnerAdaptV1InnerServiceSMOImpl.queryPartnerAdapts(partnerAdaptDto);

            if (ListUtil.isNull(partnerAdaptDtos)) {
                state = "10003";
                throw new CmdException("合作商不支持");
            }

            IPartnerOrderVerificationAdapter partnerAdapter = ApplicationContextFactory.getBean(partnerAdaptDtos.get(0).getOrderBeanImpl(), IPartnerOrderVerificationAdapter.class);

            if (partnerAdapter == null) {
                state = "10003";
                throw new CmdException("合作商未实现");
            }


            boolean refund = reqJson.getBoolean("refund");
            ResponseEntity responseEntity = null;
            //todo 走退货流程
            if (refund) {
                responseEntity = partnerAdapter.refundOrder(reqJson);
                context.setResponseEntity(responseEntity);
                return;
            }

            responseEntity = partnerAdapter.createOrder(reqJson);
            context.setResponseEntity(responseEntity);

        }finally {
            savePartnerLog("订单核销", reqJson.toJSONString(), context.getResponseEntity().toString(), state, valueDtos.get(0).getPpId(), valueDtos.get(0).getShopId());

        }
    }

    private void savePartnerLog(String action, String reqJson, String resJson, String state, String ppId, String shopId) {

        PartnerLogPo partnerLogPo = new PartnerLogPo();
        partnerLogPo.setPersonName("-");
        partnerLogPo.setResParam(resJson);
        partnerLogPo.setLogId(GenerateCodeFactory.getGeneratorId("11"));
        partnerLogPo.setPersonId("-1");
        partnerLogPo.setShopId(shopId);
        partnerLogPo.setReqParam(reqJson);
        partnerLogPo.setState(state);
        partnerLogPo.setLogAction(action);
        partnerLogPo.setPpId(ppId);
        partnerLogV1InnerServiceSMOImpl.savePartnerLog(partnerLogPo);

    }


}
